package com.xuhai.movie.controller;

import com.xuhai.movie.bean.Userinfo;
import com.xuhai.movie.service.UserinfoService;
import com.xuhai.movie.util.RTException;
import com.xuhai.movie.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2022/4/8/008
 */
@RestController
@RequestMapping("/user")
public class UserinfoController extends AbstractController {

    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/page")
    public ResultJson page(int page, int limit, Userinfo userinfo){
        return userinfoService.getPage(page, limit, userinfo);
    }

    @GetMapping("/info/{userId}")
    public ResultJson info(@PathVariable Integer userId){
        return userinfoService.getById(userId);
    }

    @PostMapping("/update")
    public ResultJson update(@RequestBody Userinfo userinfo){
        userinfoService.update(userinfo);
        return ResultJson.ok();
    }

    @DeleteMapping("/delete/{userId}")
    public ResultJson delete(@PathVariable Integer userId){
        if(userId.equals(getLoginUser().getUserId())){
            throw new RTException("当前登录用户不得删除！");
        }
        userinfoService.delete(userId);
        return ResultJson.ok();
    }

    @GetMapping("/info")
    public ResultJson info(){
        return ResultJson.ok().data(getLoginUser());
    }

    @PostMapping("/password")
    public ResultJson password(String password,String oldPassword){
        userinfoService.updatePassword(getLoginUser().getUserId(), password,oldPassword);
        return ResultJson.ok();
    }
}
